from optparse import Option
from sanic_wtf import SanicForm
from wtforms import PasswordField, StringField, SubmitField, SelectField,SelectMultipleField, EmailField, RadioField, HiddenField
from wtforms.validators import DataRequired, InputRequired, Length, EqualTo, ValidationError, IPAddress

from app.api_v1.models import User, Host

# https://emojipedia.org/

class OrderForm(SanicForm):
    errs = StringField(label='')
    user_id = HiddenField(label='😃用户id',
                    default=0,
                    # validators=[ DataRequired(message="🙃用户id为空,请登录")]
                    )
    
    hostsize = HiddenField(label='😃主机空间大小',
                    default=0.0,
                    # validators=[ DataRequired(message="🙃主机空间大小为空,请登录")]
                    )

    balance = HiddenField(label='😃用户余额',
                    default=0.0,
                    # validators=[ DataRequired(message="🙃金额异常,请登录")]
                    )
    
    price = HiddenField(label='😃主机价格',
                    default=0.0,
                    # validators=[ DataRequired(message="🙃金额异常,请登录")]
                    )

    hostip = SelectField(label='😃主机类型',
                        validate_choice=True,
                        validators=[ DataRequired(message="🙃主机类型未选择")],
                        render_kw={'class':'radios'})

    area = HiddenField(label='😃主机ip物理地址区域')

    loginurl = HiddenField(label='😃主机登陆url')   
             
    submit = SubmitField(label='订购😃',
                        render_kw={'class':'btn gradient'})

class HostForm(SanicForm):
    ip = StringField(label='😃主机ipv4网络地址', 
                    validators=[ DataRequired(message="🙃主机ipv4网络地址不能为空"),
                                IPAddress(ipv4=True, ipv6=False, message="🙃不是有效的主机ipv4网络地址")],
                    render_kw={'class':'box', 'placeholder':'🙃请输入ipv4格式网络地址'})
    
    def validate_ip(form, field):
        # print(f'form {form} field{field} -> data {field.data}')
        hip = Host.get_or_none(ip=field.data)
        if None != hip:
            raise ValidationError('🙃ip已存在')
    
    area = StringField(label='😃主机ip物理地址区域', 
                    validators=[ DataRequired(message="🙃主机ip物理地址区域不能为空")],
                    render_kw={'class':'box','placeholder':'🙃请输入主机ip物理地址区域'})
    
    def validate_area(form, field):
        # print(f'form {form} field{field} -> data {field.data}')
        harea = Host.get_or_none(area=field.data)
        if None != harea:
            raise ValidationError('🙃区域已存在')

    login_url = StringField(label='😃主机登陆url', 
                    validators=[ DataRequired(message="🙃主机登录url不能为空")],
                    render_kw={'class':'box','placeholder':'🙃请输入登录url'})
    
    # (0api 1自营 2国内 3国外 4不限内容 5CDN 6vpn)
    server_type = SelectMultipleField(label='😃主机类型',
                        choices=[('0', 'api节点'),('1', '自营服务器'),('2', '国内服务器'),('3', '国外服务器'),('4', '不限内容')],
                        validate_choice=True,
                        validators=[ DataRequired(message="🙃主机类型未选择")],
                        render_kw={'class':'w-100 flex justify-center', 'size': 6},
                        option_widget={}
                        )

    server_provider = StringField(label='😃ISP服务商名', 
                    validators=[ DataRequired(message="🙃ISP服务商名不能为空")],
                    render_kw={'class':'box','placeholder':'🙃请输入ISP服务商名'})

    server_url = StringField(label='😃ISP服务商后台管理地址', 
                    validators=[ DataRequired(message="🙃ISP服务商后台管理地址不能为空")],
                    render_kw={'class':'box','placeholder':'🙃请输入ISP服务商后台管理地址'})
    
    security_code = StringField(label='😃服务器ep安全码', 
                    validators=[ DataRequired(message="🙃服务器ep安全码不能为空")],
                    render_kw={'class':'box','placeholder':'🙃请输入服务器ep安全码'})
    
    server_account = StringField(label='😃服务器帐号', 
                    validators=[ DataRequired(message="🙃服务器帐号不能为空")],
                    render_kw={'class':'box','placeholder':'🙃请输入服务器帐号'})
    
    server_password = StringField(label='😃服务器密码', 
                    validators=[ DataRequired(message="🙃服务器密码不能为空")],
                    render_kw={'class':'box','placeholder':'🙃请输入服务器密码'})

    submit = SubmitField(label='添加😃',
                        render_kw={'class':'btn gradient'})

class LoginForm(SanicForm):
    name = StringField(label='😃帐号', 
                    validators=[ DataRequired(message="🙃帐号不能为空"),
                                Length(min=3, max=15, message="🙃帐号最少3位最大15位")],
                    render_kw={'class':'box', 'placeholder':'🙃请输入帐号'})
    
    def validate_name(form, field):
        # print(f'form {form} field{field} -> data {field.data}')
        user = User.get_or_none(name=field.data)
        if None == user:
            raise ValidationError('🙃用户名不存在')
        
    password = PasswordField(label='😃密码', 
                    validators=[ DataRequired(message="🙃密码不能为空"),
                                Length(min=3, max=15, message="🙃密码最少3位最大15位")],
                    render_kw={'class':'box', 'placeholder':'🙃请输入密码'})
        
    submit = SubmitField(label='登录😃',
                        render_kw={'class':'btn gradient'})


class RegisterForm(SanicForm):
    name = StringField(label='😃帐号', 
                    validators=[ DataRequired(message="🙃帐号不能为空"),
                                Length(min=3, max=15, message="🙃帐号最少3位最大15位")],
                    render_kw={'class':'box', 'placeholder':'🙃请输入帐号'})
    
    def validate_name(form, field):
        # print(f'form {form} field{field} -> data {field.data}')
        user = User.get_or_none(name=field.data)
        if user != None:
            raise ValidationError('🙃用户名已经存在')
    
    password = PasswordField(label='😃密码', 
                            validators=[ InputRequired(message="🙃密码不能为空"),
                                        Length(min=3, max=15, message="🙃密码最少3位最大15位"),
                                        EqualTo('confirm', message='🙃密码不一致')],
                            render_kw={'class':'box', 'placeholder':'🙃请输入密码'})

    confirm  = PasswordField(label='😃确认', 
                            validators=[ InputRequired(message="🙃确认密码不能为空"),
                                        Length(min=3, max=15, message="🙃确认密码最少3位最大15位"),
                                        EqualTo('password', message='🙃两次密码不一致')],
                            render_kw={'class':'box', 'placeholder':'🙃请再次输入密码'})

    # email = EmailField(label='😃邮箱',
    #                     render_kw={'class':'box', 'placeholder':'🙃邮箱非必填项'})

    submit = SubmitField(label='注册😃',
                        render_kw={'class':'btn gradient'})